home *** CD-ROM | disk | FTP | other *** search
- Path: news.eden.com!usenet
- From: Shane Sadler <nexus@eden.com>
- Newsgroups: comp.lang.c
- Subject: Re: Why is Gets() so bad?
- Date: Sat, 02 Mar 1996 23:34:42 -0700
- Organization: Eden Matrix Services
- Message-ID: <31393D81.271EBFCD@eden.com>
- References: <4hb1ie$pa7@ixnews2.ix.netcom.com>
- NNTP-Posting-Host: net-1-039.austin.eden.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; Linux 1.2.8 i586)
-
- Studcat's Big Studdog. wrote:
- >
- > Hi.. I'm not a newbie.. I don't even use Gets(), fgets(), or scanf.. I
- > use my own getstring function. My question is that everywhere I see
- > that gets() is bad. Why exactly is it so bad? Could someone please
- > explain in more detail than what the FAQ does? Thanks.
-
- You've *never* used scanf(), et al.?! Hmmmm.... If you know anything
- about memory allocation in C (and you should if you went to the trouble
- to write your own function to avoid some of these problems), it should
- be obvious why the use of something like gets() is not desirable. Maybe
- you should use some of those functions just to fill in that strange
- lacuna in your C education.
-
- In more detail than the FAQ? Well, actually, not much detail is needed.
- How about if I quote the Ten Commandments for C Programmers:
-
- [quote]
- V. Thou shalt check the array bounds of all strings (indeed, all
- arrays), for surely where thou typest "foo" somone someday shall type
- "supercalifragilisticexpialidocious".
- [end quote]
-
- Basically, gets() doesn't prevent a user from overstepping those bounds.
- So its use can cause you a lot of problems if someone were to type in
- that long entry above and you have made provisions only for a few bytes.
- The function fgets(), on the other hand, explicitly sets the maximum
- input allowed so the bounds are not exceeded. That's not much detail,
- but it's the whole story. I hope it helps.
-
- BTW, if anyone is interested in Henry Spencer's annotated version of the
- Ten Commandments for C Programmers, I believe it's still available at
- ftp.spies.com. Look in /Library/Techdoc/Language. A most excellent
- document. Ignore the advice therein at your own peril.
-
- -- Shane
- ===================================================================
- S. M. Sadler
- e-mail: nexus@eden.com
- Web: http://www.eden.com/~nexus
-